﻿CREATE PROCEDURE [branding].[ResolveValue]
	@resourceId varchar(200),
	@tag varchar(50),
	@subTag varchar(50),
	@brand varchar(200)
AS
	declare @value table
	(
		ValueId int,
		Brand varchar(200)
	)

	insert	@value (ValueId, Brand)
	select	ID as ValueId,
			Brand
	from	branding.Value
	where	ResourceId = @resourceId and
			(@tag is null or @tag = Tag) and
			(@subTag is null or @subTag = SubTag);

	select	top 1
			bv.ID,
			CAST('value' as varchar(10)) as ObjectType,
			bv.Tag,
			bv.SubTag,
			bv.ResourceId,
			bv.Brand,
			bv.ContentLength,
			bv.TypeName,
			bv.SerializedValue
	from	@value as v
	inner   join branding.Value as bv on bv.ID = v.ValueId
	where   CHARINDEX(v.Brand, @brand) != 0
	order   by LEN(v.Brand) desc
RETURN 0